package zzzxb.ui;

import zzzxb.generation.ConditionalRules;
import zzzxb.generation.Format;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * @author Zzzxb  2019/8/10 18:42
 * @description:
 */
public class DirectoryTOChoose extends JFrame{
    private DirectoryToChooseJpanel directoryToChooseJpanel;

    public DirectoryTOChoose() throws HeadlessException {
        directoryToChooseJpanel = new DirectoryToChooseJpanel();
        this.setTitle("博客_post目录选择");
        this.setSize(400,120);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setContentPane(directoryToChooseJpanel);
    }
}

class DirectoryToChooseJpanel extends JPanel{
    JTextField filePathField;
    JButton chooseButton;
    JButton submit;
    JFileChooser jfc;

    public DirectoryToChooseJpanel () {
        this.setLayout(null);
        jfc = new JFileChooser();
        filePathField = new JTextField();
        chooseButton = new JButton("choose");
        submit = new JButton("ok");
        init();
        listen();
    }

    public void init() {
        filePathField.setBounds(20,10,360,25);
        chooseButton.setBounds(20, 45, 100,30);
        submit.setBounds(280, 45, 100,30);

        this.add(filePathField);
        this.add(submit);
        this.add(chooseButton);
    }

    public void listen() {
        chooseButton.addActionListener(e -> chooseButtonListen());
        submit.addActionListener(e -> submitButtonListen());
    }

    public void chooseButtonListen() {
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.showDialog(new JLabel(), "选择");
        filePathField.setText(
                new File(jfc.getSelectedFile().getName()).getAbsolutePath());
    }

    public void submitButtonListen() {
        ConditionalRules.format.setFilePath(filePathField.getText()); // 获取地址
    }
}
